﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xinkui.WebModel.BasicControl;
using Xinkui.WebModel.Cache;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;
using Xinkui.WebModel.Model;
using Xinkui.WebModel;
using CommonFramework.Utility;
using Xinkui.WebModel.Helper;


public partial class UserControl_UserCenter_UC_BasicInfomation : AccountControl {
    protected string ImageLogo = string.Empty;
    protected string ImagePhoto = string.Empty;
    protected void Page_Load(object sender, EventArgs e) {

        if (!IsPostBack) {


            this.dropCountry.DataSource = CountryCache.List().Values;
            this.dropCountry.DataTextField = "Name";
            this.dropCountry.DataValueField = "CountryId";
            this.dropCountry.DataBind();

            List<SelectItem> provinceSource = new List<SelectItem>();
            List<SelectItem> citySource = new List<SelectItem>();

            provinceSource.Add(new SelectItem {
                Text = "Please Select",
                Value = "0",
                IsSelect = true
            });

            citySource.Add(new SelectItem {
                Text = "Please Select",
                Value = "0",
                IsSelect = true
            });
            var userInfo = Singleton<UserInfoMaintainDataAccess>.Instance.UserInfoMaintain_View(this.CurrentLogin.UserID);
            var companyInfo = Singleton<CompanyInfoMaintainDataAccess>.Instance.CompanyInfoMaintain_View(this.CurrentLogin.CompanyID);

            if (companyInfo.CountryID > 0) {
                var provinceDatas = ProvinceCache.ListByCountry(companyInfo.CountryID);
                if (provinceDatas != null && provinceDatas.Count > 0) {
                    provinceSource.First().IsSelect = false;
                    provinceSource.AddRange(provinceDatas.Select(u => new SelectItem {
                        Text = u.Province_EN,
                        Value = u.ProvinceID.ToString(),
                        IsSelect = u.ProvinceID == companyInfo.ProvinceID
                    }));
                    var cityDatas = CityCache.ListByProvince(companyInfo.CityID);
                    if (cityDatas != null && cityDatas.Count > 0) {
                        citySource.First().IsSelect = false;
                        citySource.AddRange(cityDatas.Select(u => new SelectItem {
                            Text = u.City_EN,
                            Value = u.CityID.ToString(),
                            IsSelect = companyInfo.CityID == u.CityID
                        }));
                    }
                }
            }

            this.rptProvince.DataSource = provinceSource;
            this.rptProvince.DataBind();

            this.rptCity.DataSource = citySource;
            this.rptCity.DataBind();

            BindDictSource(this.DropJobTitle, DictTypeEnum.JobTitle, userInfo.JobTitle);
            BindDictSource(this.dropDownCapital, DictTypeEnum.MonenyRange, companyInfo.RegisterCapital);
            BindDictSource(this.dropDownExportRatio, DictTypeEnum.ExportRatio, companyInfo.ExportRatio);
            BindDictSource(this.dropDownNumberOfRDEmployees, DictTypeEnum.NumberOfRdEmployees, companyInfo.NumberofRDEmployees);
            BindDictSource(this.dropDownNumOfEmployees, DictTypeEnum.NumberOfEmployees, companyInfo.NumberofEmployees);
            BindDictSource(this.dropDownOwnership, DictTypeEnum.OwnerShipType, companyInfo.OwnershipType);
            BindDictSource(this.dropDownSales, DictTypeEnum.MonenyRange, companyInfo.TotalAnnualSales);
            BindDictSource(this.dropDownIsExport, DictTypeEnum.IsExport, companyInfo.IsExport);

            var exportMarket = DictCache.Dict((int)DictTypeEnum.MainBuyingArea).Values;



            this.rptChkMarket.DataSource = exportMarket.Select(u => new SelectItem {
                IsSelect = !string.IsNullOrEmpty(companyInfo.ExportMarket) && companyInfo.ExportMarket.IndexOf(u.Idx.ToString()) >= 0,
                Text = u.Value,
                Value = u.Idx.ToString()
            });
            this.rptChkMarket.DataBind();
            rbtnMale.Checked = userInfo.Gender == 1;
            rbtnFemale.Checked = userInfo.Gender == 2;
            this.txtCompanyName.Text = companyInfo.CompanyName;
            this.txtAddress.Text = companyInfo.StreetAddress;
            this.txtCeo.Text = companyInfo.Ceo;
            this.txtDepartment.Text = userInfo.Department;
            this.txtDescription.Text = companyInfo.CompanyDescription;
            this.txtEmail.Text = userInfo.Email;
            this.txtFaxNum.Text = companyInfo.FaxNum;
            this.txtFirstname.Text = userInfo.FirstName;
            this.txtLastName.Text = userInfo.LastName;
            this.txtMainProducts.Text = companyInfo.MainProduct;
            this.txtMobile.Text = userInfo.Mobile;
            this.txtPhoneNum.Text = companyInfo.PhoneNum;
            this.txtTypicClient.Text = companyInfo.TypicalClients;
            this.txtWebSite.Text = companyInfo.WebSite;
            this.txtYearEstablished.Text = companyInfo.YearEstabished;
            this.txtZipCode.Text = companyInfo.PostCode;
            if (!string.IsNullOrEmpty(companyInfo.Logo)) {
                ImageLogo = Xinkui.Common.FileUpload.GetDirectoryPath(companyInfo.Logo, Xinkui.Common.Enum.ESourceType.FromCompanyLogo);
            }

            if (!string.IsNullOrEmpty(companyInfo.ImagePhoto)) {
                ImagePhoto = Xinkui.Common.FileUpload.GetDirectoryPath(companyInfo.ImagePhoto, Xinkui.Common.Enum.ESourceType.FromCompanyImage);
            }


        }
    }


    protected void BindDictSource(DropDownList dropDown, DictTypeEnum t, int value) {
        var dictValues = DictCache.Dict((int)t);
        if (dictValues == null || dictValues.Count <= 0) {
            dictValues = new Dictionary<int, Xinkui.WebModel.Cache.View.DictView>();
            dictValues.Add(-1, new Xinkui.WebModel.Cache.View.DictView {
                Idx = -1,
                DictType = (int)t,
                Value = "Please Select"
            });
        }
        dropDown.DataSource = dictValues.Values;
        dropDown.DataTextField = "Value";
        dropDown.DataValueField = "Idx";
        dropDown.DataBind();
        dropDown.Items.Insert(0, new ListItem {
            Text = "Please Select",
            Value = "-1"
        });
        if (dictValues.ContainsKey(value)) {
            dropDown.SelectedValue = value.ToString();
        }
        else {
            dropDown.SelectedIndex = 0;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs args) {
        var userInfo = Singleton<UserInfoMaintainDataAccess>.Instance.UserInfoMaintain_View(this.CurrentLogin.UserID);
        var companyInfo = Singleton<CompanyInfoMaintainDataAccess>.Instance.CompanyInfoMaintain_View(this.CurrentLogin.CompanyID);
        companyInfo.CompanyName = this.txtCompanyName.Text;
        companyInfo.MainProduct = this.txtMainProducts.Text;
        companyInfo.CountryID = ConvertHelper.ToType<int>(this.dropCountry.SelectedValue, 0);
        int provinceVal = WebHelper.GetFormValue<int>("dropProvince");
        int cityVal = WebHelper.GetFormValue<int>("dropCity");
        string hidfilepath = WebHelper.GetFormValue<string>("hidfilepath");
        string hidfilepath2 = WebHelper.GetFormValue<string>("hidfilepath2");
        if (!string.IsNullOrEmpty(hidfilepath)) {
            companyInfo.Logo = hidfilepath;
            ImageLogo = Xinkui.Common.FileUpload.GetDirectoryPath(hidfilepath, Xinkui.Common.Enum.ESourceType.FromCompanyLogo);
        }
        if (!string.IsNullOrEmpty(hidfilepath2)) {
            companyInfo.ImagePhoto = hidfilepath2;
            ImagePhoto = Xinkui.Common.FileUpload.GetDirectoryPath(hidfilepath2, Xinkui.Common.Enum.ESourceType.FromCompanyImage);
        }
        companyInfo.ProvinceID = provinceVal;
        companyInfo.CityID = cityVal;
        companyInfo.StreetAddress = this.txtAddress.Text;
        companyInfo.PostCode = this.txtZipCode.Text;
        companyInfo.PhoneNum = this.txtPhoneNum.Text;
        companyInfo.FaxNum = this.txtFaxNum.Text;
        companyInfo.WebSite = this.txtWebSite.Text;
        companyInfo.CompanyDescription = this.txtDescription.Text;
        companyInfo.NumberofEmployees = ConvertHelper.ToType<int>(this.dropDownNumOfEmployees.SelectedValue, -1);
        companyInfo.NumberofRDEmployees = ConvertHelper.ToType<int>(this.dropDownNumberOfRDEmployees.SelectedValue, -1);
        companyInfo.Ceo = this.txtCeo.Text;
        companyInfo.YearEstabished = this.txtYearEstablished.Text;
        companyInfo.RegisterCapital = ConvertHelper.ToType<int>(this.dropDownCapital.SelectedValue, -1);
        companyInfo.TotalAnnualSales = ConvertHelper.ToType<int>(this.dropDownSales.SelectedValue, -1);
        companyInfo.IsExport = ConvertHelper.ToType<int>(this.dropDownIsExport.SelectedValue, -1);
        companyInfo.ExportRatio = ConvertHelper.ToType<int>(this.dropDownExportRatio.SelectedValue, -1);
        companyInfo.OwnershipType = ConvertHelper.ToType<int>(this.dropDownOwnership.SelectedValue, -1);

        companyInfo.ExportMarket = WebHelper.GetFormValue<string>("chkMarket");
        companyInfo.TypicalClients = this.txtTypicClient.Text;
        companyInfo.CompanyEmail = this.txtEmail.Text;
        userInfo.Email = this.txtEmail.Text;
        userInfo.UpdateDate = DateTime.Now;
        companyInfo.UpdateDate = DateTime.Now;
        companyInfo.UpdateIp = WebHelper.UserIP;
        userInfo.Department = this.txtDepartment.Text;
        userInfo.JobTitle = ConvertHelper.ToType<int>(this.DropJobTitle.SelectedValue, -1);
        userInfo.Mobile = this.txtMobile.Text;
        userInfo.FirstName = this.txtFirstname.Text;
        userInfo.LastName = this.txtLastName.Text;

        userInfo.Gender = this.rbtnMale.Checked ? 1 : (this.rbtnFemale.Checked ? 2 : 0);
        companyInfo.AuditStatus = (short)AuditStatusEnum.Change;


        if (Singleton<UserInfoMaintainDataAccess>.Instance.UserInfoMaintain_Update(userInfo) &&
            Singleton<CompanyInfoMaintainDataAccess>.Instance.CompanyInfoMaintain_Update(companyInfo)) {
            UIHelper.ShowSuucessTips("Infomation Saved");
        }
        else {
            UIHelper.ShowSuucessTips("Save Error");
        }
    }
}